package com.langll.ai.stock.controller;

import com.langll.ai.stock.model.Ticket;
import com.langll.ai.stock.service.ITicketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 股票查询票据信息 控制器
 * </p>
 *
 * @author langll
 * @since 2025-04-21
 */
@Slf4j
@RestController
@RequestMapping("/stock/ticket")
@RequiredArgsConstructor
@Tag(name = "股票查询票据接口", description = "股票查询票据管理相关接口")
public class TicketController {

    private final ITicketService ticketService;

    @Operation(summary = "创建股票查询票据")
    @PostMapping("/create")
    public ResponseEntity<Ticket> createTicket(@RequestBody Ticket ticketRequest) {
        Assert.notNull(ticketRequest, "票据请求不能为空");
        Assert.hasText(ticketRequest.getSymbol(), "股票代码不能为空");
        
        String symbol = ticketRequest.getSymbol();
        String period = ticketRequest.getPeriod() != null ? ticketRequest.getPeriod() : "daily";
        String startDate = ticketRequest.getStartDate();
        String endDate = ticketRequest.getEndDate();
        String adjust = ticketRequest.getAdjust();
        Float timeout = ticketRequest.getTimeout();
        
        Ticket ticket = ticketService.createTicket(symbol, period, startDate, endDate, adjust, timeout);
        return ResponseEntity.ok(ticket);
    }

    @Operation(summary = "根据ID获取票据")
    @GetMapping("/{id}")
    public ResponseEntity<Ticket> getTicketById(@PathVariable Long id) {
        Assert.notNull(id, "ID不能为空");
        
        Ticket ticket = ticketService.selectById(id);
        return ResponseEntity.ok(ticket);
    }

    @Operation(summary = "更新票据状态")
    @PutMapping("/{id}/status")
    public ResponseEntity<Boolean> updateTicketStatus(
            @PathVariable Long id,
            @RequestParam Integer status,
            @RequestParam(required = false) Long resultId) {
        Assert.notNull(id, "ID不能为空");
        Assert.notNull(status, "状态不能为空");
        
        boolean result = ticketService.updateTicketStatus(id, status, resultId);
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "更新票据信息")
    @PutMapping("/{id}/update")
    public ResponseEntity<Ticket> updateTicket(
            @PathVariable Long id,
            @RequestBody Ticket ticketRequest) {
        Assert.notNull(id, "ID不能为空");
        Assert.notNull(ticketRequest, "票据请求不能为空");
        
        // 确保ID一致
        ticketRequest.setId(id);
        
        // 验证必要的字段
        Assert.hasText(ticketRequest.getSymbol(), "股票代码不能为空");
        
        Ticket updatedTicket = ticketService.updateTicket(ticketRequest);
        return ResponseEntity.ok(updatedTicket);
    }

    @Operation(summary = "根据股票代码查询票据")
    @GetMapping("/symbol/{symbol}")
    public ResponseEntity<List<Ticket>> getTicketsBySymbol(@PathVariable String symbol) {
        Assert.hasText(symbol, "股票代码不能为空");
        
        List<Ticket> tickets = ticketService.getTicketsBySymbol(symbol);
        return ResponseEntity.ok(tickets);
    }

    @Operation(summary = "获取所有票据列表")
    @GetMapping("/all")
    public ResponseEntity<List<Ticket>> getAllTickets() {
        List<Ticket> tickets = ticketService.getAllTickets();
        return ResponseEntity.ok(tickets);
    }

    @Operation(summary = "获取未执行的票据列表")
    @GetMapping("/pending")
    public ResponseEntity<List<Ticket>> getPendingTickets() {
        List<Ticket> tickets = ticketService.getPendingTickets();
        return ResponseEntity.ok(tickets);
    }

    @Operation(summary = "获取执行中的票据列表")
    @GetMapping("/running")
    public ResponseEntity<List<Ticket>> getRunningTickets() {
        List<Ticket> tickets = ticketService.getRunningTickets();
        return ResponseEntity.ok(tickets);
    }

    @Operation(summary = "获取已完成的票据列表")
    @GetMapping("/completed")
    public ResponseEntity<List<Ticket>> getCompletedTickets() {
        List<Ticket> tickets = ticketService.getCompletedTickets();
        return ResponseEntity.ok(tickets);
    }

    @Operation(summary = "删除票据")
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> deleteTicket(@PathVariable Long id) {
        Assert.notNull(id, "ID不能为空");
        
        int result = ticketService.deleteById(id);
        return ResponseEntity.ok(result);
    }
}